<?php

require_once 'common.inc.php';
include './deploy/alarm_state.php';

$tracker = new dbstuff;
$tracker -> connect($dsdbhost, $dsdbuser, $dsdbpw, $traname, $dspconnect);
$tra_db = $tracker;
// TrackerTactic
error_reporting(E_ALL);
if (empty($action))$action = 'list';
if ('list' == $action) {
	$listip = array();
	$port = array();
	$oldip = null;

	$sql = "select * from area order by `describe` asc";
	$query = $dsdb -> query($sql);

	while ($result = $dsdb -> fetch_array($query)) {
		if(!in_array($result['port'],$port)){
			$port[]=$result['port'];
		}
		if (!in_array($result['describe'], $listip)) {
			$oldip = $result['describe'];
			$listip[] = $oldip;
		} 
	} 

	for($i = 0;$i < ceil(count($imgseednum_acs) / 3);$i++) {
		$imgseednum_acs_tmp = array();
		for($x = $i * 3;$x < $i * 3 + 3;$x++) {
			$imgseednum_acs_tmp[] = $imgseednum_acs[$x];
		} 
		in_array('<img src="images/breakdown.jpg" />', $imgseednum_acs_tmp)?$seednum_acs[] = '<img src="images/breakdown.jpg" />':$seednum_acs[] = '<img src="images/normal.jpg" />';
	} 

	for($i = 0;$i < ceil(count($imgspace_acs) / 3);$i++) {
		$imgspace_acs_tmp = array();
		for($x = $i * 3;$x < $i * 3 + 3;$x++) {
			$imgspace_acs_tmp[] = $imgspace_acs[$x];
		} 
		in_array('<img src="images/breakdown.jpg" />', $imgspace_acs_tmp)?$space_acs[] = '<img src="images/breakdown.jpg" />':$space_acs[] = '<img src="images/normal.jpg" />';
	} 

	$imgport_acs = batdiscover($listip, $port);
	
	for($i = 0;$i < ceil(count($imgport_acs) / 3);$i++) {
		$imgport_acs_tmp = array();
		for($x = $i * 3;$x < $i * 3 + 3;$x++) {
			$imgport_acs_tmp[] = $imgport_acs[$x];
		} 
		in_array('<img src="images/breakdown.jpg" />', $imgport_acs_tmp)?$port_acs[] = '<img src="images/breakdown.jpg" />':$port_acs[] = '<img src="images/normal.jpg" />';
	}


	include_once MCC_ROOT . '/templates/msystem/alarm_acs.html';
} elseif ('ports' == $action) {
	$sql = "select * from area where `describe`='$describe' order by port asc";
	$query = $dsdb -> query($sql);
	while ($result = $dsdb -> fetch_array($query)) {
		$list[] = $result;
		$port[]= $result['port'];
	} 
	$listip[]=$describe;	
	$imgport_acs = batdiscover($listip, $port);
	include_once MCC_ROOT . '/templates/msystem/alarm_acs.html';
} 

function discover($host, $port) {
	$fp = fsockopen($host, $port, $errno, $errstr, 1);
	if (!$fp) {
		return '<img src="images/breakdown.jpg" />';
	} else {
		return '<img src="images/normal.jpg" />';
	} 
} 
function batdiscover($host, $port) {
	foreach($host as $list) {
		foreach($port as $value) {
			$result[] = discover($list, $value);
		} 
	} 
	return $result;
} 

?>